package com.gitee.kamismile.stone.basic.data.infrastructure.repository.handler.r2.convert;



import net.sf.json.JSONObject;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.Objects;

@ReadingConverter
public class DateConverter implements Converter<ZonedDateTime, Date> {

    @Override
    public Date convert(ZonedDateTime zonedDateTime) {
        if(Objects.isNull(zonedDateTime)){
            return null;
        }
        return Date.from(zonedDateTime.toInstant());
    }
    @WritingConverter
    public static class DateToZonedDateTimeConverter implements Converter<Date,ZonedDateTime> {

        @Override
        public ZonedDateTime convert(Date source) {
            if(Objects.isNull(source)){
                return null;
            }
           return source.toInstant().atZone(ZoneId.systemDefault());
        }
    }
}

